﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class CS_CPXH_Add : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lblTitle.Text = "新增产品型号";
                initValue();
            }
        }

        private void initValue()
        {
            CS_CPXSet_Dao daoCPX = new CS_CPXSet_Dao();
            List<CS_CPX> lsCPLX = daoCPX.QueryAll();
            this.ddlCPXBH.DataSource = lsCPLX;
            this.ddlCPXBH.DataValueField = "BH";
            this.ddlCPXBH.DataTextField = "MC";
            this.ddlCPXBH.DataBind();
            this.ddlCPXBH.Items.Insert(0, new ListItem("请选择", "0"));
        }


        /// <summary>
        /// 清空所有TextBox内容
        /// </summary>
        private void ClearControlsValue()
        {
            this.lbResult.Text = "";
            this.ddlCPXBH.SelectedValue = "0";
            this.ddlCPMCBH.SelectedValue = "0";
            this.tbMC.Text = "";
            this.tbBH.Text = "";
        }

        protected void btnSave0_Click(object sender, ImageClickEventArgs e)
        {
            this.lbResult.Text = "";

            if (this.ddlCPXBH.SelectedValue == "0")
            {
                this.lbResult.Text = "请选择产品线";
                return;
            }

            if (this.ddlCPMCBH.SelectedValue == "0")
            {
                this.lbResult.Text = "请选择产品名称";
                return;
            }

            if (this.tbBH.Text.Trim().Equals(""))
            {
                this.lbResult.Text = "请输入编号";
                return;
            }

            CS_CPXH entity = new CS_CPXH()
            {
                CPXBH = this.ddlCPXBH.SelectedValue,
                CPMCBH = this.ddlCPMCBH.SelectedValue,
                BH = this.tbBH.Text.Trim(),
                MC = this.tbMC.Text.Trim()
            };

            string resultMessage = "";
            try
            {
                CS_CPXHSet_Dao dao = new CS_CPXHSet_Dao();

                if (dao.Exist(entity.CPXBH, entity.CPMCBH, entity.BH, entity.MC))
                {
                    resultMessage = "同样编号或名称的产品型号已存在";
                    return;
                }

                int result = dao.Add(entity);

                if (result == 1)
                {
                    resultMessage = "保存成功";
                    ClearControlsValue();
                }
            }
            catch (Exception ex)
            {
                resultMessage = "保存失败" + ex.Message;
            }
            finally
            {
                this.lbResult.Text = resultMessage;
            }
        }

        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
            ClearControlsValue();
        }

        protected void btnReturn_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/CS_CPXH_Query.aspx");
        }

        protected void ddlCPXBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            CS_CPMCSet_Dao daoCPMC = new CS_CPMCSet_Dao();
            List<CS_CPMC> lsCPMC = daoCPMC.QueryByCPX(this.ddlCPXBH.SelectedValue);
            this.ddlCPMCBH.DataSource = lsCPMC;
            this.ddlCPMCBH.DataValueField = "BH";
            this.ddlCPMCBH.DataTextField = "MC";
            this.ddlCPMCBH.DataBind();
            this.ddlCPMCBH.Items.Insert(0, new ListItem("请选择", "0"));
        }
    }
}